var userRoleName;
var updateRoleId;
$(function () {
    userRoleName = $.getUrlParam("roleName");
    var roleId = $.getUrlParam("id");
    updateRoleId = roleId;
    //通过角色ID获取该角色下有哪些资源权限
    $.ajax({
        url: "../role/getPermissionByRoleId.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data: {'roleId':roleId},
        success: function (result) {
            editPermission(result.data);
        }
    });

    $("#close").click(function () {
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(index);
    });
});

var setting = {
    view: {
        selectedMulti: false
    },data: {
        key: {
            name: "permissionName",
            url : false
        },
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: 0
        }
    },callback: {

    },check: {
        enable: true,
        chkStyle: "checkbox",
        chkboxType: { "Y": "ps", "N": "ps" }
    }
};

function editPermission(role){
    $("#saveRolePermission").click(function(){
        updateRolePermission(role);
    });
    $.ajax({
        url: "../role/getAllPermission.htm",
        cache: false,
        type: "post",
        dataType: "json",
        success: function (result) {
            var data = result.data;
            if (!data){
                return;
            }
            for(var i=0;i<data.length;i++){
                $(data[i]).attr("icon",null);
                if (role){
                    for (var j=0;j<role.length;j++){
                        if (data[i].id == role[j].id){
                            $(data[i]).attr("checked",true);
                        }
                    }
                }
            }
            $.fn.zTree.init($("#ztreeId"), setting, eval(result.data));
        }
    });
}

//更新角色-权限关系
function updateRolePermission(role){
    var getSelectedPermission = $.fn.zTree.getZTreeObj("ztreeId").getCheckedNodes(true);
    var perms = [];
    for(var i = 0 ; i < getSelectedPermission.length ; i ++){
        perms.push(getSelectedPermission[i].id);
    }
    layer.confirm('你确定要更新【' + userRoleName + '】角色权限吗？', {
        btn: ['更新', '取消'] //按钮
    }, function () {
        //更新角色
        $.ajax({
            url: "../role/updateRolePermission.htm",
            cache: false,
            type: "post",
            dataType: "json",
            data: {"roleId": updateRoleId,"roles":perms},
            success: function (result) {
                layer.msg(result.message, {icon: 1});
                parent.$table.bootstrapTable('refresh', {url: '../role/getRolesPage.htm'});
                closeForm();
            }
        });
    }, function () {
    });
}

//关闭窗口
function closeForm() {
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}
